<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="公司名称" prop="lxr">
        <el-input
            v-model="queryParams.name"
            placeholder="请输入公司名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人" prop="lxr">
        <el-input
            v-model="queryParams.lxr"
            placeholder="请输入联系人"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系方式" prop="phone">
        <el-input
            v-model="queryParams.phone"
            placeholder="请输入联系方式"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Plus" v-hasPermi="['mxerp:supplier:add']" @click="handleAdd">新增</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="supplierList">
      <el-table-column label="公司名称" align="center" prop="name" />
      <el-table-column label="联系人" align="center" prop="lxr" />
      <el-table-column label="联系方式" align="center" prop="phone" />
      <el-table-column label="微信" align="center" prop="wx" />
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['mxerp:supplier:edit']"></el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['mxerp:supplier:remove']"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改供货商对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="supplierRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="公司名称" prop="name">
          <el-input v-model="form.name" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="联系人" prop="lxr">
          <el-input v-model="form.lxr" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系方式" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系方式" />
        </el-form-item>
        <el-form-item label="微信" prop="wx">
          <el-input v-model="form.wx" placeholder="请输入微信" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="位置" prop="wz">
          <!-- <el-input v-model="form.wz" placeholder="请输入位置" /> -->
          <el-upload
              class="avatar-uploader"
              action="http://192.144.201.205:1000/prod-api/mxerp/img/mx_image"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar"/>
            <el-icon v-else class="avatar-uploader-icon">
              <Plus/>
            </el-icon>
          </el-upload>
        </el-form-item>
        <el-form-item label="简称" prop="jcname">
          <el-input v-model="form.jcname" placeholder="请输入简称" />
        </el-form-item>
        <el-form-item label="经营类目" prop="jycode">
          <!-- <el-input v-model="form.jycode" placeholder="请输入经营类目" /> -->
          <el-select v-model="form.jycode" multiple filterable allow-create style="width:100%;">
            <el-option
                v-for="item in jylist"
                :key="item.id"
                :label="item.val"
                :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Supplier">
import { listSupplier, getSupplier, delSupplier, addSupplier, updateSupplier } from "@/api/mxerp/supplier";
import { getParamByPid } from "@/api/bydll/param";


const { proxy } = getCurrentInstance();

const supplierList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const imageUrl = ref("");
const jylist = ref([]);


const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 20,
    name: null,
    lxr: null,
    phone: null,
    wx: null,
    address: null,
    wz: null,
    jcname: null,
    jycode: null,
    addtime: null,
    upTime: null
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询供货商列表 */
function getList() {
  loading.value = true;
  listSupplier(queryParams.value).then(response => {
    supplierList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

function getSelData(){
  let param = {"pid":"23"};
  getParamByPid(param).then(resp => {
    jylist.value = resp.data
  })
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    lxr: null,
    phone: null,
    wx: null,
    address: null,
    wz: null,
    remark: null,
    jcname: null,
    jycode: null,
    addtime: null,
    upTime: null
  };
  imageUrl.value = "";
  proxy.resetForm("supplierRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加供货商";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getSupplier(_id).then(response => {
    form.value = response.data;
    open.value = true;
    imageUrl.value = response.data.wz;

    /******************已经类目*********进行处理多选的问题***********************************/
    let jycode = form.value.jycode;
    jycode = jycode.replace("[","").replace("]","");
    let zslist = jycode.split(",");
    let zsvalue = [];
    if(zslist.length > 1){
      for(var i = 0;i < zslist.length;i++ ){
        zsvalue[i] = parseInt(zslist[i]);
      }
      form.value.jycode = zsvalue;
    }else{
      zsvalue[0] = parseInt(zs);
      form.value.jycode = zsvalue;
    }


    title.value = "修改供货商";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["supplierRef"].validate(valid => {
    if (valid) {
      form.value.jycode = String(form.value.jycode);
      if (form.value.id != null) {
        updateSupplier(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addSupplier(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除供货商编号为"' + _ids + '"的数据项？').then(function() {
    return delSupplier(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('mxerp/supplier/export', {
    ...queryParams.value
  }, `supplier_${new Date().getTime()}.xlsx`)
}


getSelData();
getList();



/****上传图片***/
const beforeAvatarUpload = (rawFile) => {
  if (rawFile.type !== 'image/jpeg') {
    ElMessage('图片仅支持jpg格式的图片！')
    return false
  } else if (rawFile.size / 1024 / 1024 > 5) {
    ElMessage.error('图片不能大于5MB!')
    return false
  }
  return true
}

const handleAvatarSuccess = (response, uploadFile) => {
  imageUrl.value = uploadFile.response.imgUrl;
  form.value.wz = uploadFile.response.imgUrl;
}


</script>


<!-- 上传图片的css样式 -->
<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  text-align: center;
}

.avatar{
  width: 100px;
  height: 100px;
}
</style>
